<template>
  <el-dialog title="个人资料" :visible.sync="perPop" width="400px"  append-to-body>
    <!-- 头像区域 -->
    <div class="avatar-container">
     <!-- <el-avatar :src="form.avatarUrl" :size="90">
        <i class="el-icon-user" v-if="!form.avatarUrl"></i>
      </el-avatar> -->
      <div class="avatar">
        <!-- 实际可替换成动态头像，这里用占位 -->
        <el-upload :action="$baseUrl + '/common/upload'"  :headers="headers"  ref="uploadRef" list-type="picture-card"
         :on-success="handleSuccess"
         	:on-change="(file, fileList) => { handlePictureCardPreview(file, fileList) }"
        	:auto-upload="true" :show-file-list="false">
        	<!-- <i class="el-icon-plus" v-show="!form.avatarUrl"></i> -->
        	<img width="100%" :src="avarUrl || require('@/assets/image/head.svg')" alt="avatar">
          <!-- <img :src="form.avatarUrl || require('@/assets/image/head.svg')" alt="avatar" /> -->
        </el-upload>
      </div>
      <!--  <el-button
        type="primary"
        icon="el-icon-plus"
        size="mini"
        style="margin-top: 8px;"
        @click="handleAvatarUpload"
      ></el-button> -->
      <el-button type="text" @click="handleModifyPassword">修改密码</el-button>
    </div>
    <!-- 表单区域 -->
    <el-form :model="form" label-width="80px">
      <el-form-item label="昵称">
        <el-input v-model="form.nickname"></el-input>
      </el-form-item>
      <el-form-item label="电话号">
        <el-input v-model="form.phone"></el-input>
      </el-form-item>
      <el-form-item label="账号">
        <el-input v-model="form.account" disabled></el-input>
      </el-form-item>
    </el-form>
    <!-- 底部按钮 -->
    <span slot="footer" class="dialog-footer">
      <el-button @click="perPop = false">取消</el-button>
      <el-button type="primary" @click="handleSubmit">完成</el-button>
    </span>
    <!-- item -->
     <PasswordModal  ref="setPasswordModal"/>
  </el-dialog>
</template>

<script>
   import PasswordModal from './PasswordModal.vue';
  export default {
    name: 'PersonalInfoDialog',
    components: {
      PasswordModal
    },
    data() {
      return {
        headers:{
          Authorization:sessionStorage.getItem('token')
        },
        perPop: false, // 控制弹窗显示隐藏，可根据父组件传值调整
        avarUrl:'',
        form: {
           "account":'',
           "nickname": "低空经济",
            "phone": "15803888688",
            "email": "",
            "head": "",
            "filename":''
        },
      }
    },
    methods: {
      open() {
        this.perPop = true;
        let userInfo=JSON.parse(sessionStorage.getItem("userInfo"))
        this.form.nickname=userInfo.nickname
        this.form.phone=userInfo.phone
         this.form.head=userInfo.head
         if(userInfo.head){
           this.avarUrl= this.$baseUrl + '/upload/'+userInfo.head
         }
          this.form.account=userInfo.username
      },
      handlePictureCardPreview(file) {
      	// this.avarUrl = file.url;
      },
      handleSuccess(res){
        console.log('上传成功',res)
        this.form.head=res.data.filename
        this.avarUrl='/upload/'+res.data.filename
      },
      handleSubmit() {
        let parms=this.form
        this.$httpApi.updateProfile(parms).then((res) => {
          if (res.success) {
              this.$message.success('操作成功');
              this.getUserInfo()
          }
          else{
            this.$message.error(res.msg);
          }
        })
        this.perPop = false; // 提交后关闭弹窗，可根据实际交互调整
      },
      // 获取用户信息
      getUserInfo(){
          this.$httpApi.getLoginUserInfo().then((res) => {
            if (res.success) {
               sessionStorage.setItem("userInfo", JSON.stringify(res.data));
               this.form.nickname=res.data.nickname
               this.form.phone=res.data.phone
                this.form.head=res.data.head
                 this.avarUrl=res.data.head
                  this.form.account=res.data.username
                   this.$root.Bus.$emit('refreshList',res.data)
            } else {
              this.$message.error(res.msg);
            }
          });
      },
      handleModifyPassword() {
        // updateProfile
        this.$nextTick(()=>{
          this.$refs.setPasswordModal.open()
        })
        // 可跳转修改密码页面或弹出修改密码弹窗等逻辑
        console.log('点击了修改密码，可扩展修改密码逻辑',this.$refs.setPasswordModal);
      }
    }
  }
</script>

<style scoped>
  .avatar-container {
    display: flex;
    text-align: center;
    flex-direction: column;
    align-items: center;
    justify-content: center;
  }

  .dialog-footer {
    text-align: right;
  }
  >>>.el-avatar{
    background: transparent;
  }
  .avatar img {
    width:120px;
      height: 120px;
    border-radius: 50%;
  }

  >>>.el-upload--picture-card{
    border: 0;
  }
</style>
